package com.btkjgc.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.ScoreRule;
import com.btkjgc.models.extend.ScoreRuleModel;
import com.btkjgc.models.extend.ScoreRuleReceiveModel;
import com.btkjgc.service.intf.IntegralManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

/**
 * Created by _ESE on 2015/2/9.
 */
@Component
public class IntegralManageService_Impl extends BaseService_Impl
		implements IntegralManageService_Intf {
 
	static Logger logger = Logger.getLogger(IntegralManageService_Impl.class);

	@SuppressWarnings("unchecked")
	@Override
	public AjaxObj insertScoreRuleModel(
			ScoreRuleModel scoreRuleModel){
		
		AjaxObj ajaxObj=new AjaxObj();
		ajaxObj.setResult(true);
		ScoreRule scoreRule = new ScoreRule();

  
		try {
			PropertyUtils.copyProperties(scoreRule,
					scoreRuleModel);
			scoreRule.setDel(0);
			scoreRule.setCreatedate(new Date());
			scoreRule.setOperdate(new Date());
			scoreRule.setOperid(getLoginUser().getId());
			HibernateUtil.save(scoreRule);

		} catch (Exception e) {
			e.printStackTrace();
			ajaxObj.setResult(false);
			ajaxObj.setMsg("添加时出错");
		}
		
		return ajaxObj;
	}

	@Override
	public void setListData(HttpServletRequest request, ModelAndView mv,
			int pageIndex, int totalRecord,
			List<ScoreRuleReceiveModel> scoreRuleReceiveModelList){
		// TODO Auto-generated method stub

 
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}
 
		mv.addObject("score_rule_receive_list", scoreRuleReceiveModelList);


		mv.addObject("currentpage", pageIndex);
		mv.addObject("totalRecord", totalRecord);
	}

	@Override
	public void saveIntegralule(ScoreRuleModel scoreRuleModel) {
		
		ScoreRule scoreRule =new ScoreRule();
		try {
			PropertyUtils.copyProperties(scoreRule, scoreRuleModel);
			scoreRule.setCreatedate(new Date());
			scoreRule.setOperdate(new Date());
			scoreRule.setOperid(getLoginUser().getId());
		} catch (IllegalAccessException | InvocationTargetException
				| NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HibernateUtil.save(scoreRule);
	}

}
